package com.thread.threadfirst.SpringThreadPool;

import com.thread.threadfirst.ThreadFirstApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 测试AsyncExceptionDemo类中的方法
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ThreadFirstApplication.class)
public class AsyncApplicationWithAsyncConfigurerTests {

    @Autowired
    AsyncExceptionDemo asyncExceptionDemo;

    @Test
    public void contextLoads() throws ExecutionException, InterruptedException {
        asyncExceptionDemo.asyncInvokeSimplest();
        asyncExceptionDemo.asyncInvokeWithParameter("testtt");
        Future<String> future = asyncExceptionDemo.asyncInvokeReturnFuture(100);  //虽然这里的参数输入的是int类型的100，但是在这个方法中有throw new IllegalArgumentException("a");这句意思是已经抛出了IllegalArgumentException异常 异常信息是a，所以下面才会输出error-IllegalArgumentException。
        //get(long timeout, TimeUnit unit) 在设置时间内未返回结果，会直接抛出异常TimeoutException，messages为null。
        System.out.println(future.get());  //输出error-IllegalArgumentException。
        Future<List<String>> result = asyncExceptionDemo.test();
        System.out.println(result.get());  //输出[a, b]
    }
}
